/*
持久化
   1：值改变了就存储到浏览器（存）
   2：初始值读取浏览器存储(取)

vuex持久化插件使用
   vuex-persistedstate
   1:下载  npm i vuex-persistedstate -f
   2:导入  src/store/index.js
      import persistedstate from 'vuex-persistedstate'
   3:{
      state:{},
      .....,
      plugins:[persistedstate({
        paths:['模块名.属性名']
      })]
   }
*/
import { userProfile } from '@/api/mine'
export default {
  namespaced: true,
  state () {
    return {
      num: 0,
      tokenObj: {},
      userInfo: {}
      // num: +localStorage.getItem('num') || 0,
      // tokenObj: JSON.parse(localStorage.getItem('tokenObj')) || {}
    }
  },
  //   state: {
  //     num: +localStorage.getItem('num') || 0,
  //     tokenObj: JSON.parse(localStorage.getItem('tokenObj')) || {}
  //   },
  mutations: {
    setNum (state, value) {
      state.num = value
      // localStorage.setItem('num', value)
    },
    // 修改tokenObj
    setTokenObj (state, value) {
      state.tokenObj = value
      // localStorage.setItem('tokenObj', JSON.stringify(value))
    },
    // 修改userInfo
    setUserInfo (state, value) {
      state.userInfo = value
    },
    // 退出登录操作
    logout (state) {
      state.tokenObj = {}
      state.userInfo = {}
    }
  },
  actions: {
    // 用户获取用户信息
    async getUserInfo (ctx) {
      // console.log('ctx', ctx)
      if (!ctx.state.userInfo.id) {
        const res = await userProfile()
        ctx.commit('setUserInfo', res.data)
      }
    }
  },
  getters: {
    xxx (state) {
      return state.userInfo
    }
    // getters:{'user/xxx':...}
    // this.$store.getters['user/xxx']
  }
}
